<?php
namespace app\admin\controller;
use app\common\model\ModuleSetting as ModuleSettingModel;

class ModuleSetting extends AdminCommon
{
    public function index($id){
        $params = $this->request->param();
        $moduleModel = new ModuleSettingModel();
        $info = $moduleModel->get($params['id']);
        if ($this->request->isAjax() && $this->request->isPost() && isset($params['req_sub'])) {
            $files = $this->request->file('slide');
            $old_content = unserialize($info['content']);
            if($files){
                $path = 'setting';
                $file_arr = [];
                if(!is_array($files)){
                    $file_arr[0] = $files;
                }else{
                    $file_arr = $files;
                }
                foreach ($file_arr as $k => $v){
                    $file_info = $v->move(ROOT_PATH . 'public' . DS . 'upload'.DS.$path);
                    $old_content['slide'][$k] = DS . 'upload' . DS . $path .DS. $file_info->getSaveName();
                }
            }else{

            }
            $save_data = [];
            $save_data['slide'] = $old_content['slide'];
            $save_data['side_admin_title'] = $params['side_admin_title'];
            $save_data['side_home_title'] = $params['side_home_title'];

            $moduleModel = new ModuleSettingModel();
            if ($moduleModel->allowField(true)->save(['content'=>serialize($save_data)], ['id' => $id]) !== false) {
                $this->success('编辑成功', url('admin/module_setting/index',['id'=>$id]));
            } else {
                $this->error('编辑失败');
            }
        } else {

            $info['content'] = unserialize($info['content']);
            $this->assign('info', $info);
            return $this->fetch();
        }
    }
}